package main

import (
	ctrl "huzhen/controllers"
	uc "huzhen/controllers/user" //
	"huzhen/controllers/sc"
	"net/http"
	//"huzhen/controllers/db"
)

/* 转发器  */
/*type Handler interface {源码
	ServeHTTP(ResponseWriter, *Request)
}*/
type dispatchHandler struct {
	handler http.Handler//调度处理程序接口
}

func (a *dispatchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	a.handler.ServeHTTP(w, r)
}

func Dispatch(w http.ResponseWriter, r *http.Request) {
	/*for key,_:= range ctrl.HMAP{
		fmt.Println(key)
	}*/
	if hf, exists := ctrl.HMAP[r.URL.Path]; exists {
		// 基本权限管理
		hf(w, r)
	}

}

func main() {
	initHandlers()
	//db.InitModels()
	ah := &dispatchHandler{http.HandlerFunc(Dispatch)}//http://blog.csdn.net/songbohr/article/details/16350137
	println("Listening on port 8080")
	http.ListenAndServe(":8080", ah)
}

func initHandlers() {
	// 控制器初始化
	sc.InitHandlers() //用户
	uc.InitHandlers() //用户
}


